﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace atmsystem_ss
{
    public partial class frmDeposit_Currency : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dtAcc = new DataTable("tblAccount");
        private DataTable dtTran = new DataTable("tblTransaction");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        public Int32 a;
        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = "Select * from tblTransaction";
            da.SelectCommand = command;
            da.Fill(dtTran);
        }
        private bool IsNumber(String str)
        {
            foreach (Char c in str)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }

        private bool kiemtra()
        {
            kt = true;

            if (txtID_Account.Text == "")
            {
                txtID_Account.Focus();
                MessageBox.Show("Ban chua nhap Account ID !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (IsNumber(Convert.ToString(txtID_Account.Text)) == false)
            {
                txtID_Account.Focus();
                MessageBox.Show("Account ID la mot chuoi so nguyen, moi ban nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (tontai() == true)
            {

                if (MessageBox.Show("Account ID khong ton tai, ban vui long kiem tra lai Account ID !", "Thong bao", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
                {
                    txtID_Account.Text = "";
                    txtCurrency_Add.Text = "";
                    txtID_Account.Focus();
                }
                else
                {
                    Close();
                    Dispose();
                    disconnect();
                    frmAdmin _frmAdmin = new frmAdmin();
                    _frmAdmin.Show();
                }
                kt = false;
            }
            else if (txtCurrency_Add.Text == "")
            {
                txtCurrency_Add.Focus();
                MessageBox.Show("Ban chua nhap Currency Add !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (IsNumber(Convert.ToString(txtCurrency_Add.Text)) == false)
            {
                txtCurrency_Add.Focus();
                MessageBox.Show("Currency Add ban nhap khong phai la so, moi ban nhap lai  !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }
        private bool tontai()
        {
            dtAcc.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblAccount where fldID_Account LIKE '%'+@fldID_Account+'%' ";
            command.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = txtID_Account.Text;
            da.SelectCommand = command;
            da.Fill(dtAcc);
            if (dtAcc.Rows.Count > 0)
            {
                return false;
            }
            return true;

        }
        public frmDeposit_Currency()
        {
            InitializeComponent();
        }

        private void frmUpdate_Currency_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }

        private void btncomplete_Click(object sender, EventArgs e)
        {
            kiemtra();


            if (kt == true)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "InsertTransaction_UpdateAccount_Deposit";
                cmd.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = txtID_Account.Text;
                cmd.Parameters.Add("@fldCurrency_Transaction", SqlDbType.Int, 50).Value = Convert.ToInt32(txtCurrency_Add.Text);
                SqlDataReader daReader = cmd.ExecuteReader();
                dtTran.Load(daReader);
                MessageBox.Show("Complete", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtID_Account.Text = "";
                txtCurrency_Add.Text = "";
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            frmAdmin _frmAdmin = new frmAdmin();
            _frmAdmin.Show();
        }
    }
}
